<template>
  <div>
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Import')"
      >导入</el-button
    >
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Export')"
      >导入</el-button
    >
    <el-button
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Upload')"
      >上传</el-button
    >
    <el-button v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Audit')"
      >审核</el-button
    >
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="value"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="label" label="权限名称" sortable>
      </el-table-column>
      <el-table-column prop="URL" label="菜单URL" sortable> </el-table-column>
      <el-table-column prop="value" label="操作" sortable>
        <template slot-scope="data">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="PerUpd(data.row.value)"
            v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Update')"
            circle
          ></el-button>
          <el-button
            type="danger"
            @click="PerDel(data.row.value)"
            icon="el-icon-delete"
            v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Delete')"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      type="danger"
      @click="OpenPerDia"
      plain
      v-if="PerButton.ButtonValue.split(',').some((x) => x == 'Add')"
      >添加菜单</el-button
    >
    <el-dialog :visible.sync="dialogFormVisiblePerAdd">
      <PerAddView @ClosePerAdd="ClosePerAdd"></PerAddView>
    </el-dialog>

    <el-dialog
      v-if="dialogFormVisiblePerUpd"
      :visible.sync="dialogFormVisiblePerUpd"
    >
      <PerUpdView :PerId="PerId" @ClosePerUpd="ClosePerUpd"></PerUpdView>
    </el-dialog>
  </div>
</template>

<script>
import PerAddView from "../../components/PermissionComponet/PermissionAddComp.vue";
import PerUpdView from "../../components/PermissionComponet/PermissionUpdComp.vue";
export default {
  components: {
    PerAddView,
    PerUpdView,
  },
  data() {
    return {
      tableData: [],
      PerId: 0,
      dialogFormVisiblePerAdd: false,
      dialogFormVisiblePerUpd: false,
      PerButton: "",
    };
  },
  created() {
    this.GetPermissionInfo();
    this.getRolePerM();
  },
  methods: {
    PerDel(Id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete(
              "http://localhost:5255/api/Permission/PermissionDel?Id=" + Id
            )
            .then((res) => {
              if (res.data.Success) {
                this.$message.success(res.data.ErrorInfo);
                this.GetPermissionInfo();
              } else {
                this.$message.error(res.data.ErrorInfo);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    PerUpd(Id) {
      this.PerId = Id;
      this.dialogFormVisiblePerUpd = true;
    },
    OpenPerDia() {
      this.dialogFormVisiblePerAdd = true;
    },

    ClosePerAdd() {
      this.dialogFormVisiblePerAdd = false;
      this.GetPermissionInfo();
    },
    ClosePerUpd() {
      this.dialogFormVisiblePerUpd = false;
      this.GetPermissionInfo();
    },
    GetPermissionInfo() {
      this.axios
        .get("http://localhost:5255/api/Permission/GetPermission")
        .then((res) => {
          this.tableData = res.data.Data;
        });
    },
    getRolePerM() {
      this.axios
        .get("http://localhost:5255/api/User/GetRolePeimiss", {
          params: {
            UserId: sessionStorage.UserId,
            PermId: this.$route.query.Id,
          },
        })
        .then((res) => {
          this.PerButton = res.data.Data;
        });
    },
  },
};
</script>

<style>
</style>